package com.wenhuitec.android.tang.ui.dialog;

import static java.lang.Thread.sleep;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.wenhuitec.android.tang.R;

public class WithDialog extends AppCompatActivity {

    private ImageView m_imgInsideCircle;  //内圆
    private ImageView m_imgOuterCircle;   //外圆

    private Animation m_animInsideCircle; //内圆动画
    private Animation m_animOuterCircle;  //外圆动画

    private AnimationDrawable m_frameAnim;

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_with);
        rotateAnimInit();

        mContext = this;
        RequestUtils.jiesuan(mContext, new MyObserverRes<String>(mContext, false) {
            @Override
            public void onSuccess(String result) {
//                checkJiesuan();
                handler.postDelayed(runnable, 1000);//触发定时器
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                checkJiesuan();
            }
        });


    }

    private void checkJiesuan() {
        handler.postDelayed(runnable, 1000);//触发定时器

    }


    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //todo what you want
//            handler.postDelayed(runnable, 1000);
            RequestUtils.checkJiesuan(mContext, new MyObserverRes<String>(mContext, false) {
                @Override
                public void onSuccess(String result) {
                    Toast.makeText(mContext, "结算完成", Toast.LENGTH_LONG).show();
                    stopAnimation();
                    finish();
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    checkJiesuan();
//                    try {
//                        sleep(1000);
//
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                    }
                }
            });

        }
    };



    private void rotateAnimInit() {
        m_imgInsideCircle = (ImageView) findViewById(R.id.imgInsideCircle);
        m_imgOuterCircle = (ImageView) findViewById(R.id.imgOuterCircle);

        m_animInsideCircle = AnimationUtils.loadAnimation(this, R.anim.inside_rotate_anim);
        m_animInsideCircle.setInterpolator(new LinearInterpolator()); //匀速动画

        m_animOuterCircle = AnimationUtils.loadAnimation(this, R.anim.outer_rotate_anim);
        m_animOuterCircle.setInterpolator(new LinearInterpolator()); //匀速动画


        //开始动画
        startAnimation();
    }

    //开始动画
    private void startAnimation() {
        m_imgInsideCircle.startAnimation(m_animInsideCircle);
        m_imgOuterCircle.startAnimation(m_animOuterCircle);
//        ImageView imgView = (ImageView) findViewById(R.id.frameAnim);
//        m_frameAnim = (AnimationDrawable) imgView.getBackground();
//        m_frameAnim.start();
    }

    //
    //停止动画
    private void stopAnimation() {
        m_imgInsideCircle.clearAnimation();
        m_imgOuterCircle.clearAnimation();

//        m_frameAnim.stop();
    }
}
